package com.darkduck.fcap;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;

public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
    private static final String TAG = PreferencesActivity.class.getSimpleName();
    
    public static final String KEY_CAPTURE_MODE = "preferences_capture_mode";    
    public static final String KEY_VIBRATE = "preferences_vibrate";
    public static final String KEY_PLAY_BEEP = "preferences_play_beep";
    public static final String KEY_FRONT_LIGHT = "preferences_front_light";
    public static final String KEY_ACCURACY = "preferences_accuracy";
    public static final String KEY_BORDER = "preferences_border"; 
    public static final String KEY_BORDER_WIDTH = "preferences_border_width";
    public static final String KEY_BORDER_COLOR = "preferences_border_color"; 
    
    public static final String CONFIDENCE_LOW = "0.2";
    
    private CheckBoxPreference border;
    private ListPreference borderWidth;
    private ListPreference borderColor;
    private String[] borderWidthArray;
    private String[] borderColorArray;
    
    static int[] BORDER_COLOR = {Color.WHITE, Color.BLACK, Color.BLUE, Color.CYAN,
                        Color.GRAY, Color.GREEN, Color.MAGENTA, Color.RED, Color.YELLOW};
    static int[] BORDER_WIDTH = {5, 10, 15};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences); 
        
        PreferenceScreen preferences = getPreferenceScreen();
        preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        border = (CheckBoxPreference) preferences.findPreference(KEY_BORDER);
        borderWidth = (ListPreference) preferences.findPreference(KEY_BORDER_WIDTH);
        borderColor = (ListPreference) preferences.findPreference(KEY_BORDER_COLOR);
        //set summary
        borderWidthArray = this.getResources().getStringArray(R.array.preferences_border_width_entries);        
        borderColorArray = this.getResources().getStringArray(R.array.preferences_border_color_entries);
        borderWidth.setSummary(borderWidthArray[Integer.valueOf(preferences.getSharedPreferences().getString(KEY_BORDER_WIDTH, "1"))]);
        borderColor.setSummary(borderColorArray[Integer.valueOf(preferences.getSharedPreferences().getString(KEY_BORDER_COLOR, "0"))]);
    }
    
    public static boolean isBorderEnable(SharedPreferences pref) {
        return pref.getBoolean(KEY_BORDER, true);
    }
    
    public static int getBorderWidth(SharedPreferences pref) {
        int width = Integer.valueOf(pref.getString(KEY_BORDER_WIDTH, "0"));
        return BORDER_WIDTH[width]; 
    }
    
    public static int getBorderColor(SharedPreferences pref) {
        int color = Integer.valueOf(pref.getString(KEY_BORDER_COLOR, "0"));
        return BORDER_COLOR[color];        
    }
    


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        boolean isBorderEnable = border.isChecked();
        borderWidth.setEnabled(isBorderEnable);
        borderColor.setEnabled(isBorderEnable); 
        
        Log.i(TAG, "width:"+borderWidthArray[Integer.valueOf(sharedPreferences.getString(KEY_BORDER_WIDTH, "1"))]);

        borderWidth.setSummary(borderWidthArray[Integer.valueOf(sharedPreferences.getString(KEY_BORDER_WIDTH, "1"))]);  
        borderColor.setSummary(borderColorArray[Integer.valueOf(sharedPreferences.getString(KEY_BORDER_COLOR, "0"))]);
    }

}
